<template>
  <div class="login">
    <el-form
      ref="form"
      class="login-form"
      :model="form"
      label-width="80px"
      label-position="top"
      :rules="rules"
    >
      <el-form-item label="用户名" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit" class="login-btn" :loading="isLoginLoading"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import { Form } from 'element-ui'
import { login } from '@/service/user'
export default Vue.extend({
  data () {
    // 18201288771 111111
    return {
      form: {
        phone: '18201288771',
        password: '111111'
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '手机号不符合规范', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { max: 16, message: '密码最长16位', trigger: 'blur' }
        ]
      },
      isLoginLoading: false
    }
  },
  methods: {
    async onSubmit () {
      try {
        await (this.$refs.form as Form).validate(async (valid:boolean) => {
          if (valid) {
            this.isLoginLoading = true
            // const { data } = await request({
            //   headers: { 'content-type': 'application/x-www-form-urlencoded' },
            //   method: 'POST',
            //   url: '/front/user/login',
            //   data: qs.stringify(this.form)// axios默认发送application/json格式
            // })

            const { data } = await login(this.form)
            this.isLoginLoading = false
            if (data.state !== 1) {
              return this.$message.error(data.message)
            } else {
              // this.$message.success(data.message)
              this.$store.commit('setUser', data.content)
              this.$router.push((this.$route.query.redirect as string) || '/')
            }
          }
        })
      } catch (error) {

      }
    }
  }
})
</script>
<style scoped lang="scss">
.login {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-form {
  width: 280px;
  background: #fff;
  padding: 20px;
  border-radius: 5px;
}
.login-btn {
  width: 100%;
}
</style>
